<?php

namespace App\Controller;

use App\Model\Product;
use App\Model\Category;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use App\AsyncQueue\MaterialAsyncQueue;
use Hyperf\Di\Annotation\Inject;
use League\Flysystem\Filesystem;
use App\Libs\Oss;
use Darabonba\OpenApi\Models\Config;
use Hyperf\Collection\Arr;

class ProductController extends AbstractController
{
    /**
     * @var MaterialAsyncQueue
     */
    #[Inject]
    protected MaterialAsyncQueue $materialAsyncQueue;
     /**
     * @var Filesystem
     */
    #[Inject]
    private Filesystem $filesystem;
    public function index()
    {
        $page_size = $this->request->input('page_size', 10);
        $name = $this->request->input('name', '');
        $category_id = $this->request->input('category_id', '');
        $model = Product::with(['category', 'layouts']);
        if($name) $model->where('name', 'like', "%{$name}%");
        if($category_id) {
            $model->whereHas('category', function($query) use($category_id){
                $query->where('id', $category_id);
            });
        }

        $list = $model->orderByDesc('id')->paginate($page_size);
        
        return $this->success($list);
    }

    public function category(){
        $list = Category::with('children')->orderBy('listorder')->orderByDesc('id')->get();
        return $this->success($list);
    }

    public function show($id)
    {
        $row = UserMaterial::where('user_id', $this->request->user->id)->findOrFail($id);
        return $this->success($row);
    }

    public function store()
    {
        $data = $this->request->all();
        $data['user_id'] = $this->request->user->id;
        $data['category_id'] = intval($this->request->input('category_id', 0));
        $row = UserMaterial::create($data);
        $this->materialAsyncQueue->push(params: $row->toArray());
        return $this->success($row);
    }

    public function update($id)
    {
        $userCategory = UserMaterial::with('category')->where('user_id', $this->request->user->id)->findOrFail($id);
        $update = Arr::only($this->request->all(), ['name', 'category_id', 'tags']);
        $userCategory->update($update);
        // $userCategory->category;
        return $this->success($userCategory);
    }

    #[DeleteMapping(path: '{id}')]
    public function destroy($id)
    {
        if($id == 0) {
            $ids = $this->request->input('ids', []);
            if (empty($ids)) {
                return $this->error('请选择要删除的素材');
            }
            $ret = UserMaterial::where('user_id', $this->request->user->id)->whereIn('id', $ids)->delete();
            return $this->success($ret);
        }
        $userCategory = UserMaterial::where('user_id', $this->request->user->id)->findOrFail($id);
        $ret = $userCategory->delete();
        return $this->success($ret);
    }
}
